package com.moonce.api.service.impl;

import com.moonce.api.mapper.UserMapper;
import com.moonce.api.model.User;
import com.moonce.api.model.vo.PaginationVO;
import com.moonce.api.model.vo.ResultJson;
import com.moonce.api.model.vo.UserVO;
import com.moonce.api.service.UserService;
import com.moonce.api.util.CommonUtils;
import com.moonce.api.util.Pagination;
import com.moonce.api.util.constant.Code;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 马路遥(229010812 @ qq.com)
 * @version V1.0
 * @Title: springmvc-demo
 * @Package com.moonce.api.service.impl
 * @Description: User服务接口实现层
 * @Date 2017-12-14 11:45
 */
@Service("UserService")
public class UserServiceImpl implements UserService {
    /*调用UserMapper*/
    @Resource
    private UserMapper userMapper;

    /**
     * 添加用户
     * @param username
     * @param password
     * @param name
     * @param email
     * @param sex
     * @return
     */
    @Override
    public String add(String username,String password, String name, String email, byte sex) {
        //判断用户名是否被注册
        User user = new User();
        user.setUsername(username);
        List<User> userList = userMapper.listAll(user);
        if(userList.size()>0){
            return ResultJson.toJson(Code.ERROR_OPERATION,Code.IS_EXIST_ACCOUNT_MSG,Code.NULL_STR);
        }
        user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(CommonUtils.encryptPassword(password));//加密密码
        user.setName(name);
        user.setSex(sex);
        //插入数据库
        Integer result = userMapper.insertSelective(user);
        if(result>0){
            return ResultJson.toJson(Code.SUCCESS,Code.SAVE_SUCC_MSG,Code.NULL_STR);
        }
        return  ResultJson.toJson(Code.FAILED,Code.SAVE_FAIL_MSG,Code.NULL_STR);
    }
    /**
     *  修改用户信息
     * @param id
     * @param username
     * @param password
     * @param name
     * @param email
     * @param sex
     * @return
     * @throws Exception
     */
    @Override
    public String update(Integer id, String username, String password, String name, String email, byte sex) {

        return null;
    }

    /**
     * 用户列表分页查询
     * @param id
     * @param username
     * @param email
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public String list(Integer id, String username, String email ,Integer pageNo ,Integer pageSize) {
        Pagination<User> pagination = new Pagination<>();
        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        params.put("username",username);
        pagination.setParams(params);
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        List<User> users = userMapper.listByPage(pagination);
        List<UserVO> userVOS = UserVO.getList(users);
        PaginationVO<UserVO> paginationVO = new PaginationVO<>();
        paginationVO.setList(userVOS);
        paginationVO.setPageNo(pagination.getPageNo());
        paginationVO.setTotal(pagination.getTotal());
        paginationVO.setTotalPage(pagination.getTotalPage());
        return ResultJson.toJson(Code.SUCCESS,Code.FIND_SUCC_MSG,paginationVO);
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @Override
    public String delete(Integer id) {
        return null;
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public String sigIn(String username, String password) {
        return null;
    }
}
